// 小程序开发api接口统一配置
const API_BASE_URL = "http://39.107.10.236:85/prod-api"; // 主域名s
const subdomain = "/app";

const API_QINIU_URL = "http://39.107.10.236:85/prod-api";


const request = (url, method, data) => {
	let _url = API_BASE_URL + subdomain + url;
	let token = uni.getStorageSync("token");
	return new Promise((resolve, reject) => {
		uni.request({
			url: _url,
			method: method,
			data: data,
			header: {
				"Content-Type": "application/json",
				Authorization: "Bearer " + token,
			},
			success(request) {
				if (request.data.code == 401) {
					uni.showToast({
						title: "请重新登录",
						icon: "none",
						success() {
							setTimeout(() => {
								uni.navigateTo({
									url: "/pages/login/login",
								});
							}, 2000);
						},
					});
				}
				resolve(request.data);
			},
			fail(error) {
				console.log(error);
				reject(error);
				uni.showToast({
					title: "请求超时",
					icon: "none",
				});
			},
			complete(aaa) {},
		});
	});
};
/**
 * 小程序的promise没有finally方法，自己扩展下
 */
Promise.prototype.finally = function(callback) {
	var Promise = this.constructor;
	return this.then(
		function(value) {
			Promise.resolve(callback()).then(function() {
				return value;
			});
		},
		function(reason) {
			Promise.resolve(callback()).then(function() {
				throw reason;
			});
		}
	);
};

// export default{
module.exports = {
	request,
	//获取图片地址
	getPath() {
		return API_QINIU_URL;
	},
	//获取请求地址
	getPathApi() {
		return API_BASE_URL;
	},

	//登录方法
	register: data => request('/register', 'post', data),
	//获取用户信息
	getInvitationByCode: data => request('/index/getInvitationByCode', 'get', data),
       

}